package org.dant.ant.extension.net;

import org.dant.ant.extension.tasks.internal.RemoteCommand;
import org.dant.ant.extension.tasks.internal.RemoteResponse;
import org.dant.ant.extension.net.DefaultDispatcher;

import org.dant.ant.extension.net.repository.ProcessRepository;

public class DefaultDispatcher implements Dispatcher {

  protected static ProcessRepository processRepository = ProcessRepository.getInstance();

  public static ProcessRepository getProcessRepository() {
    return processRepository;
  }
    
  public void dispatch(RemoteCommand req, RemoteResponse response) {
    try {
      Processor proc = (Processor) CommandMap.getProcessorClz(req.getName()).newInstance();
      proc.process(req, response);
    } catch (ClassNotFoundException e) {
      response.setSuccess(false);
      response.appendStdErrBuffer(e.getMessage());
      response.appendStdErrBuffer("Processing class for <" + req.getName() + "> is missing");
    } catch (InstantiationException ie) {
      response.setSuccess(false);
      response.appendStdErrBuffer(ie.getMessage());
      response.appendStdErrBuffer("Processing class for <" + req.getName() + "> can't be instanced");
    } catch (IllegalAccessException iae) {
      response.setSuccess(false);
      response.appendStdErrBuffer(iae.getMessage());
      response.appendStdErrBuffer("Processing class for <" + req.getName() + "> can't be accessed");
    }
  }
}
